package com.wangdao.wechatmall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.bo.wx.TopicListBo;
import com.wangdao.wechatmall.bean.pojo.Coupon;
import com.wangdao.wechatmall.bean.pojo.Topic;
import com.wangdao.wechatmall.bean.pojo.TopicExample;
import com.wangdao.wechatmall.bean.vo.admin.BaseDataVO;
import com.wangdao.wechatmall.bean.vo.wx.BaseDataListVO;
import com.wangdao.wechatmall.bean.vo.wx.BaseDateTopicVO;
import com.wangdao.wechatmall.mapper.TopicMapper;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ShuiYi
 * @time 2021-07-12 14:51
 */
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;
    @Override
    public BaseDataListVO topicList(TopicListBo topicListBo) {
        PageHelper.startPage(topicListBo.getPage(), topicListBo.getSize());

        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> topicPageInfo = new PageInfo<>();
        long total = topicPageInfo.getTotal();

        return BaseDataListVO.ok(topics,total);
    }

    @Override
    public List<Topic> topicRelated(Integer id) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Topic> topicList = topicMapper.selectByExample(topicExample);
        int k = 0;
        List<Topic> topics = new ArrayList<>();
        for (Topic topic : topicList) {
            topics.add(topic);
            k++;
            if (k == 4) {
                break;
            }
        }
        return topics;
    }

    @Override
    public BaseDateTopicVO topicDetail(Integer id) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIdEqualTo(id);
        Topic topic = topicMapper.selectByPrimaryKey(id);
        Object[] goods = topic.getGoods();

        return BaseDateTopicVO.ok(topic,goods);
    }
}
